package com.qd.p2p.web.action.user;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.biz.service.UserLoginService;
import com.qdlc.p2p.biz.util.UserReturnResult;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.util.HttpRequestUtil;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.UserModel;

/**
 * 用户登录
 * @author linya 2016-02-03
 */
public class UserLoginAction  extends BaseAction<User> implements ModelDriven<User>{

	@Resource
	UserLoginService userLoginService;
	
	@Resource
	UserIdentifyService userIdentifyService;

    /**
     * [登录]登录页面
     */
    @Action("/user/login")
    public String login() throws Exception {
        
    	// 如果用户登录过，自动跳转到主页面
    	User user = getSessionUser();
        if (user != null) {
            redirect("/member/main.html");
        }

        //TODO 不知到这个是干嘛用的!
        initRSAME();

        //是否在页面上显示验证码
        boolean showValidCode = userLoginService.isShowLoginValidCode();
        if(showValidCode){
        	request.setAttribute("count", 1);
        }
        
        //登录后直接进入的URL
        request.setAttribute("redirectURL", request.getParameter("redirectURL"));
        
        //TODO 不知道干嘛用的!
        request.setAttribute("openType", paramString("openType"));
        request.setAttribute("openId", paramString("openId"));
        
        return "login";
    }
    
    /**
     * 登录操作
     */
    @Action(value = "/user/doLogin")
    public void doLogin() throws Exception {
    	// 判断是否是POST请求
    	boolean isPost = HttpRequestUtil.isPostMethod(request);
    	if(!isPost){
    		printWebResult("非法登录!", false);
    	}
    	
    	// 如果是联合用户，则不需要登录
        User openUser = (User) request.getAttribute("openUser");
        if(openUser != null && openUser.getUserName() != null) {
            printWebSuccess();
            return;
        }
        
        // 获取请求参数
        UserModel userModel = new UserModel();
        userModel.setUserName(model.getUserName());
        userModel.setPwd(model.getPwd());
        String validCode = request.getParameter("validCode");
        if(validCode != null) {
            userModel.setValidCode(validCode);
        }
        
        //处理登录流程
        ReturnResult result = userLoginService.doLogin(userModel);
        
        if(result instanceof UserReturnResult){
        	User user = ((UserReturnResult)result).getUser();
        	loginSuccess(user, result);
        }
        
        
        //返回页面消息
        printWebResult(result.getErrorInfo(), result.isSuccess(), result.getExtra());
    }

    /**
     * 用户登录成功后处理
     * @param user
     * @param result
     */
    private void loginSuccess(User user, ReturnResult result){
    	result.addExtra("result", true);
    	result.addExtra("msg", "登录成功！");
    	result.addExtra("userName", user.getUserName());
    	result.addExtra("imgurl", "/avatar/" + user.getUserId() + ".jpg");
    	result.addExtra("redirectURL", paramString("redirectURL"));
    }
    
}
